﻿using Reference.Apps.Models.Entities;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Net.Http;
using System.Linq;
using System.Text;
using Windows.Data.Xml.Dom;
using System.Threading.Tasks;

namespace Reference.Apps.Models.Data
{
    public class ReferenceWebService
    {
        public async Task<ObservableCollection<CodeGroup>> GetGroups()
        {
            HttpClient client = new HttpClient();
            string xmlresponse = await client.GetStringAsync("http://referenceapp.cloudapp.net/ReferenceService.svc/Groups");
            XmlDocument response = new XmlDocument();
            response.LoadXml(xmlresponse);
            var cgroups = from g in response.GetElementsByTagName("CodeGroup")
                          select new CodeGroup
                          {
                              GroupId = Int32.Parse(g.ChildNodes[0].InnerText),
                              Name = g.ChildNodes[1].InnerText
                          };
            return new ObservableCollection<CodeGroup>(cgroups); ;
        }

        public async Task<CodeSnippet> GetSnippet(int groupId, int snippetId)
        {
            HttpClient client = new HttpClient();
            string xmlresponse = await client.GetStringAsync("http://referenceapp.cloudapp.net/ReferenceService.svc/Snippet/" + groupId + "/" + snippetId);
            XmlDocument response = new XmlDocument();
            response.LoadXml(xmlresponse);
            var cgroups = (from g in response.GetElementsByTagName("CodeSnippet")
                           select new CodeSnippet
                           {
                               Lang = g.ChildNodes.FirstOrDefault(node => node.NodeName.Equals("Lang")).InnerText,
                               Title = g.ChildNodes.FirstOrDefault(node => node.NodeName.Equals("Title")).InnerText,
                               GroupId = groupId,
                               Id = snippetId,
                               CodeGroupName = g.ChildNodes.FirstOrDefault(node => node.NodeName.Equals("GroupName")).InnerText,
                               Notes = g.ChildNodes.FirstOrDefault(node => node.NodeName.Equals("Notes")).InnerText,
                               Code = g.ChildNodes.FirstOrDefault(node => node.NodeName.Equals("Code")).InnerText.Replace("\\n", "\n").Replace("\\t", "\t")
                           }).FirstOrDefault();
            return cgroups;
        }

        public async Task<ObservableCollection<CodeSnippet>> GetRecentSnippets(int groupId)
        {
            HttpClient client = new HttpClient();
            string xmlresponse = await client.GetStringAsync("http://referenceapp.cloudapp.net/ReferenceService.svc/Snippets/Recent/" + groupId);
            XmlDocument response = new XmlDocument();
            response.LoadXml(xmlresponse);
            var cgroups = from g in response.GetElementsByTagName("CodeSnippetLight")
                          select new CodeSnippet
                          {
                              Id = Int32.Parse(g.ChildNodes.FirstOrDefault(node => node.NodeName.Equals("Id")).InnerText),
                              GroupId = groupId,
                              Lang = g.ChildNodes.FirstOrDefault(node => node.NodeName.Equals("Lang")).InnerText,
                              Title = g.ChildNodes.FirstOrDefault(node => node.NodeName.Equals("Title")).InnerText
                          };
            return new ObservableCollection<CodeSnippet>(cgroups);
        }

        public async Task<ObservableCollection<CodeGroup>> GetGroupsFull()
        {
            var response = await GetGroups();
            foreach (CodeGroup gc in response)
            {
                gc.Snippets = await GetRecentSnippets(gc.GroupId);
                if (gc.Snippets != null && gc.Snippets.Count > 0)
                    gc.LastSnippetId = gc.Snippets[gc.Snippets.Count - 1].Id;
            }
            return response;
        }

        public async Task<ObservableCollection<CodeSnippet>> GetNextSnippets(int groupId, int lastSnippetId, int count)
        {
            HttpClient client = new HttpClient();
            string xmlresponse = await client.GetStringAsync(String.Format("http://referenceapp.cloudapp.net/ReferenceService.svc/Snippets/count/{0}/{1}/{2}", count, groupId, lastSnippetId));
            XmlDocument response = new XmlDocument();
            response.LoadXml(xmlresponse);
            var cgroups = from g in response.GetElementsByTagName("CodeSnippetLight")
                          select new CodeSnippet
                          {
                              Id = Int32.Parse(g.ChildNodes.FirstOrDefault(node => node.NodeName.Equals("Id")).InnerText),
                              GroupId = groupId,
                              Lang = g.ChildNodes.FirstOrDefault(node => node.NodeName.Equals("Lang")).InnerText,
                              Title = g.ChildNodes.FirstOrDefault(node => node.NodeName.Equals("Title")).InnerText
                          };
            return new ObservableCollection<CodeSnippet>(cgroups);
        }

        public async Task<ObservableCollection<CodeSnippet>> SearchSnippets(string word)
        {
            HttpClient client = new HttpClient();
            string xmlresponse = await client.GetStringAsync(String.Format("http://referenceapp.cloudapp.net/ReferenceService.svc/Snippets/all/{0}", word));
            XmlDocument response = new XmlDocument();
            response.LoadXml(xmlresponse);
            var cgroups = from g in response.GetElementsByTagName("CodeSnippetLight")
                          select new CodeSnippet
                          {
                              Id = Int32.Parse(g.ChildNodes.FirstOrDefault(node => node.NodeName.Equals("Id")).InnerText),
                              GroupId = Int32.Parse(g.ChildNodes.FirstOrDefault(node => node.NodeName.Equals("GroupId")).InnerText),
                              Lang = g.ChildNodes.FirstOrDefault(node => node.NodeName.Equals("Lang")).InnerText,
                              Title = g.ChildNodes.FirstOrDefault(node => node.NodeName.Equals("Title")).InnerText
                          };
            return new ObservableCollection<CodeSnippet>(cgroups);
        }

    }
}
